﻿Imports System.IO
Imports System.Windows.Forms

Public Class FormProfile

    Private ep As New ErrorProvider
    Private options As ApplicationSettings

#Region " Properties "

    Private m_profile As New GameProfile
    Public Property Profile() As GameProfile
        Get
            Return m_profile
        End Get
        Set(ByVal value As GameProfile)
            m_profile = value
        End Set
    End Property

#End Region

#Region " Methods "

    Private Sub SetupControls()
        ep.BlinkStyle = ErrorBlinkStyle.NeverBlink
        cboDiscType.SelectedIndex = 0
        cboPadMode.SelectedIndex = 0
        picIcon.Image = My.Resources.psxfin.ToBitmap

        If Me.Profile Is Nothing Then
            Me.Profile = New GameProfile
        Else
            With Me.Profile
                txtName.Text = .Name
                picIcon.Image = .Icon
                txtDiscLocation.Text = .DiscLocation
                txtMemCard1.Text = .MemoryCard1
                txtMemCard2.Text = .MemoryCard2
                txtPatchLocation.Text = .PatchFile
                cboPadMode.SelectedIndex = .PadMode
            End With

            If File.Exists(txtDiscLocation.Text) Then
                cboDiscType.SelectedIndex = 0
            Else
                cboDiscType.SelectedIndex = 1
            End If
        End If
    End Sub

    Private Sub SaveProfileData()
        With Me.Profile
            .Name = txtName.Text
            .Icon = TryCast(picIcon.Image, Bitmap)
            .DiscLocation = txtDiscLocation.Text
            .MemoryCard1 = txtMemCard1.Text
            .MemoryCard2 = txtMemCard2.Text
            .PatchFile = txtPatchLocation.Text
            .PadMode = cboPadMode.SelectedIndex
        End With
    End Sub

    Private Function AreFieldsValid() As Boolean
        Dim result As Boolean = True
        ep.SetError(txtName, "")
        ep.SetError(txtDiscLocation, "")
        ' Validate Name
        If Not (FileHandler.IsNameValid(txtName.Text.Trim)) Then
            ep.SetError(txtName, "Invalid Name")
            result = False
        End If
        ' Validate Disc Image
        If cboDiscType.SelectedIndex = 0 Then
            If (txtDiscLocation.Text = "") Or (Not File.Exists(txtDiscLocation.Text)) Then
                ep.SetError(txtDiscLocation, "Invalid Filename")
                result = False
            End If
        End If
        ' Validate Disc Drive
        If cboDiscType.SelectedIndex = 1 Then
            If (txtDiscLocation.Text = "") Or (Not File.Exists(txtDiscLocation.Text)) Then
                ep.SetError(txtDiscLocation, "Invalid Path")
                result = False
            End If
        End If
        Return result
    End Function

    Public Overloads Function ShowDialog(ByVal profileName As String) As Windows.Forms.DialogResult
        Me.Profile = FileHandler.LoadProfile(profileName)
        Return MyBase.ShowDialog()
    End Function

#End Region

#Region " Events "

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If Not AreFieldsValid() Then
            Exit Sub
        End If
        Me.SaveProfileData()
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub FormGameConfig_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FileHandler.LoadOptions(options)
        SetupControls()
        AreFieldsValid()
    End Sub

    Private Sub btnIcon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIcon.Click
        Dim dlgOpenIcon As New OpenFileDialog
        With dlgOpenIcon
            .Title = "Choose Icon"
            .InitialDirectory = Path.Combine(Path.GetDirectoryName(options.PsxfinExePath), "cards")
            .RestoreDirectory = True
            .Filter = "Icons (*.ico)|*.ico"
            If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                If File.Exists(.FileName) And .FileName.EndsWith(".ico") Then
                    Dim icn As New Icon(.FileName)
                    picIcon.Image = icn.ToBitmap
                End If
            End If
        End With
    End Sub

    Private Sub btnPatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPatch.Click
        Dim dlgOpenPatch As New OpenFileDialog
        With dlgOpenPatch
            .Title = "Choose Patch file"
            .InitialDirectory = Path.Combine(Path.GetDirectoryName(options.PsxfinExePath), "cdimages")
            .Filter = "Patch files (*.pff)|*.pff"
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                txtPatchLocation.Text = .FileName
            End If
        End With
    End Sub

    Private Sub btnMemCard1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMemCard1.Click
        Dim dlgOpenMemCard As New OpenFileDialog
        With dlgOpenMemCard
            .Title = "Choose Memory Card Slot 1"
            .InitialDirectory = Path.Combine(Path.GetDirectoryName(options.PsxfinExePath), "cards")
            .Filter = "Memory Cards|*.bin;*.mcd;*.mcr"
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                txtMemCard1.Text = .FileName
            End If
        End With
    End Sub

    Private Sub btnMemCard2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMemCard2.Click
        Dim dlgOpenMemCard As New OpenFileDialog
        With dlgOpenMemCard
            .Title = "Choose Memory Card Slot 2"
            .InitialDirectory = Path.Combine(Path.GetDirectoryName(options.PsxfinExePath), "cards")
            .Filter = "Memory Cards|*.bin;*.mcd;*.mcr"
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                txtMemCard2.Text = .FileName
            End If
        End With
    End Sub

    Private Sub btnDisc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisc.Click
        Select Case cboDiscType.SelectedIndex
            Case 0 ' Disc Image
                Dim dlgOpenDisc As New OpenFileDialog
                With dlgOpenDisc
                    .Title = "Choose Disc file"
                    .InitialDirectory = Path.Combine(Path.GetDirectoryName(options.PsxfinExePath), "cdimages")
                    .RestoreDirectory = True
                    .Filter = "CD Images|*.bin;*.ccd;*.cdz;*.cue;*.img;*.iso;*.mdf;*.mds;*.sub|Alcohol 120% Files (*.mdf, *.mds)|*.mdf;*.mds|BIN/CUE Files (*.bin, *.cue)|*.bin;*.cue|CDZ Files (*.cdz)|*.cdz|CloneCD files (*.ccd, *.cue, *.img, *.sub)|*.ccd;*.cue;*.img;*.sub|ISO files (*.iso)|*.iso|All files (*.*)|*.*"
                    If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                        txtDiscLocation.Text = .FileName
                    End If
                End With
            Case 1 ' Disc Drive
                Dim dlgFolder As New FolderBrowserDialog
                With dlgFolder
                    .Description = "Choose the CD/DVD drive to use for playing pSX."
                    For Each drive As DriveInfo In My.Computer.FileSystem.Drives
                        If drive.DriveType = DriveType.CDRom Then
                            .SelectedPath = drive.RootDirectory.FullName
                            Exit For
                        Else
                            .SelectedPath = My.Computer.FileSystem.Drives(0).RootDirectory.FullName
                        End If
                    Next drive
                    If .ShowDialog = Windows.Forms.DialogResult.OK Then
                        txtDiscLocation.Text = .SelectedPath
                    End If
                End With
        End Select
        AreFieldsValid()
    End Sub

    Private Sub txtName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtName.TextChanged
        AreFieldsValid()
    End Sub

    Private Sub txtDisc_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDiscLocation.TextChanged
        ep.SetError(txtDiscLocation, "")
        If (cboDiscType.SelectedIndex = 0) And (Not File.Exists(txtDiscLocation.Text)) Then
            ep.SetError(txtDiscLocation, "Invalid Filename")
        End If
        If (cboDiscType.SelectedIndex = 1) And (Not Directory.Exists(txtDiscLocation.Text)) Then
            ep.SetError(txtDiscLocation, "Invalid Path")
        End If
    End Sub

    Private Sub cboDiscType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboDiscType.SelectedIndexChanged
        AreFieldsValid()
    End Sub

#End Region

End Class
